/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// config
import { MODULE_NAME } from "./config";
// script & methods & public
import logUtils from "utils/logUtils";
import toast from "components/toast";
// store
import { state } from "./state";
// import { dataState } from "state/global";
// service
import {
  getCopyPageService,
  getCategoryLongPageService,
  editCopyService,
  deleteCopyService,
  getShelfPageService,
  getBookOrIsbnPageService,
  getSourceLongPageService,
  getRoomNoPageService,
  addCopyService,
  getShelfLongPageService,
} from "biz/Books/service";
// interface
import { ReqBookPage, ReqCopyAddPage, ReqCopyPage, ReqShelfPage } from "biz/Books/api";
// 其它

export async function getCopyPage() {
  const sp = state.getSearch();
  logUtils.info("搜索参数", sp);
  const params: ReqCopyPage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp,
  };
  // 调用服务
  state.loading = true;
  const result = await getCopyPageService(params);
  state.loading = false;
  // 成功
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}
export async function addCopy(form: any) {
  const params: ReqCopyAddPage = {
    ...form,
  };
  state.editModalLoading = true;
  const result = await addCopyService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getCopyPage();
  }
}
export async function updateCopy(form: any, copyId: number) {
  // logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  state.editModalLoading = true;
  const result = await editCopyService({
    copyId,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.editModal = false;
    getCopyPage();
  }
}
export async function delCopy(copyId: number) {
  const result = await deleteCopyService({ copyId});
  if (result.payload) {
    toast.success("删除成功");
    getCopyPage();
  }
}
export async function getBookByTitleOrIsbn(title:string,setBookList:(any:any) => void){
  const params: ReqBookPage = {
    pageNum: state.page,
    pageSize: 20,
    title
  };
  // state.loading = true;
  const result = await getBookOrIsbnPageService(params);
  // state.loading = false;
  if (result.payload) {
    setBookList(result.payload.list)
  }
}

export async function getShelfByName(shelfName:string,setShelfList:(arg0: any)=>void){
  const params: ReqShelfPage = {
    pageNum: state.page,
    pageSize: 20,
    shelfName
  };
  // state.loading = true;
  const result = await getShelfPageService(params);
  // state.loading = false;
  if (result.payload) {
    setShelfList(result.payload.list)
  }
}

// 图书分类不分页列表
export async function getCategoryLongPage() {
  // state.loading = true;
  const result = await getCategoryLongPageService({});
  // state.loading = false;
  if (result.payload) {
    state.categoryList = result.payload
      .filter((item: any) => item.parentId !== null)
  }
}
//来源不分页列表
export async function getSourceLongPage() {
  // state.loading = true;
  const result = await getSourceLongPageService({});
  // state.loading = false;
  if (result.payload) {
    state.sourceList = result.payload
  }
}
export async function getRoomLongPage() {
  // state.loading = true;
  const result = await getRoomNoPageService({});
  // state.loading = false;
  if (result.payload) {
    state.roomList = result.payload;
  }
}
export async function getShelfLongPage() {
  // state.loading = true;
  const result = await getShelfLongPageService({});
  // state.loading = false;
  if (result.payload) {
    state.shelfList = result.payload;
  }
}
export async function didMount() {
  getCategoryLongPage();
  getSourceLongPage()
  getShelfLongPage()
  getCopyPage()
}
